﻿using System.Windows;
using System.Windows.Controls;
using DevExpress.AgDataGrid;
using System.Windows.Media;

namespace Test.Silverlight.DataGrid {

    public partial class ColumnsLayout : UserControl {
        public ColumnsLayout() {
            InitializeComponent();
            grid.DataSource = EmployeesData.DataSource;
        }
        void cbAllowResize_Click(object sender, RoutedEventArgs e) {
            AgDataGridColumnHeader header = FindColumn(sender as FrameworkElement);
            header.Column.AllowResizing = ((AgToggleButton)sender).IsChecked == true ? DefaultBoolean.True : DefaultBoolean.False;
        }
        void cbFixedWidth_Click(object sender, RoutedEventArgs e) {
			AgDataGridColumnHeader header = FindColumn(sender as FrameworkElement);
            header.Column.FixedWidth = ((AgToggleButton)sender).IsChecked == true;
		}
		void cbWordWrap_Click(object sender, RoutedEventArgs e) {
			AgDataGridColumnHeader header = FindColumn(sender as FrameworkElement);
            ((AgDataGridTextColumn)header.Column).TextWrapping = (((AgToggleButton)sender).IsChecked == true) ? TextWrapping.Wrap : TextWrapping.NoWrap;
        }
        void cbWordWrap_Loaded(object sender, RoutedEventArgs e) {
			AgDataGridColumn column = ((AgDataGridColumnHeader)FindColumn(sender as FrameworkElement)).Column;
			if (!(column is AgDataGridTextColumn))
			    ((AgToggleButton)sender).Visibility = Visibility.Collapsed;
        }
        AgDataGridColumnHeader FindColumn(FrameworkElement child) {
            if (child == null) return null;
			while(!(child is AgDataGridColumnHeader) && VisualTreeHelper.GetParent(child) != null) {
				child = (FrameworkElement)VisualTreeHelper.GetParent(child);
            }
            return child as AgDataGridColumnHeader;
        }

        #region OptionsPanel
        void cbColumnAutoWidth_IsCheckedChanged(object sender, RoutedEventArgs e) {
            grid.ColumnsAutoWidth = ((CheckBox)sender).IsChecked == true;
        }
        #endregion
    }
}
